package Thread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: homechen
 * Date: 2024-03-15
 * Time: 19:22
 */
public class AtomicDemo {
    public static void main(String[] args) {
        AtomicInteger counter = new AtomicInteger(0); // 原始的值为 0
        counter.incrementAndGet(); // 1
        counter.addAndGet(5); // 6

        counter.decrementAndGet();// 5
        counter.addAndGet(-3); // 2

        int currentValue = counter.get(); // 2

        // 执行对比并替换操作
        boolean swapped = counter.compareAndSet(2, 10); // 从 2 换到 10
        int newValue = counter.get();
        System.out.println("swapped: " + swapped);

        int orignalValue = counter.incrementAndGet(); // 10 -> 11
        System.out.println("counter：" + counter);
        System.out.println("orignalValue：" + orignalValue);
    }
}
